import { MockMethod } from 'vite-plugin-mock'
import { ProjectType, ProjectStatus, DeployStatus } from '../../src/model/project-model'
import { ApiResponse } from '../../src/model/common-model'
import { faker } from '@faker-js/faker'

// 模拟项目数据
const projects = Array.from({ length: 20 }).map((_, index) => {
  const id = index + 1
  const type = Math.random() > 0.5 ? ProjectType.FRONTEND : ProjectType.BACKEND
  const status = [
    ProjectStatus.ACTIVE, 
    ProjectStatus.INACTIVE, 
    ProjectStatus.DEPRECATED, 
    ProjectStatus.DEVELOPMENT
  ][Math.floor(Math.random() * 4)]
  
  // 生成 1-3 个版本
  const versionsCount = Math.floor(Math.random() * 3) + 1
  const versions = Array.from({ length: versionsCount }).map((_, vIndex) => {
    return {
      id: vIndex + 1,
      version: `1.${vIndex}.0`,
      md5_hash: faker.string.alphanumeric(32),
      notes: faker.lorem.sentence(),
      is_current: vIndex === versionsCount - 1,
      created_at: faker.date.recent().toISOString()
    }
  })
  
  return {
    id,
    name: faker.company.name() + ' 项目',
    description: faker.lorem.paragraph(),
    type,
    status,
    git_url: `https://github.com/example/project-${id}.git`,
    base_path: type === ProjectType.FRONTEND ? `/dist/proj-${id}` : `/services/proj-${id}`,
    config_template_id: id,
    config_template: "server {\n    listen 80;\n    server_name example.com;\n}",
    startup_script: "#!/bin/bash\nnpm install\nnpm start",
    created_at: faker.date.past().toISOString(),
    updated_at: faker.date.recent().toISOString(),
    versions
  }
})

export default [
  // 获取项目列表
  {
    url: '/api/v1/projects/',
    method: 'get',
    response: (req: any) => {
      const { page = 1, page_size = 10, project_type, project_status } = req.query
      
      let filteredProjects = [...projects]
      
      // 处理筛选条件
      if (project_type) {
        filteredProjects = filteredProjects.filter(p => p.type === project_type)
      }
      
      if (project_status) {
        filteredProjects = filteredProjects.filter(p => p.status === project_status)
      }
      
      // 处理分页
      const total = filteredProjects.length
      const start = (Number(page) - 1) * Number(page_size)
      const end = start + Number(page_size)
      const projectsList = filteredProjects.slice(start, end)
      
      return {
        code: 0,
        errorMsg: '',
        data: projectsList,
        total
      }
    }
  },
  
  // 获取项目详情
  {
    url: '/api/v1/projects/:id',
    method: 'get',
    response: (req: any) => {
      const { id } = req.params
      const project = projects.find(p => p.id === Number(id))
      
      if (project) {
        return {
          code: 0,
          errorMsg: '',
          data: project
        }
      } else {
        return {
          code: 404,
          errorMsg: `项目ID ${id} 不存在`,
          data: null
        }
      }
    }
  },
  
  // 创建项目
  {
    url: '/api/v1/projects/',
    method: 'post',
    response: (req: any) => {
      const { body } = req
      const newId = projects.length + 1
      
      const newProject = {
        id: newId,
        name: body.name,
        description: body.description || '',
        type: body.type,
        status: body.status || ProjectStatus.DEVELOPMENT,
        git_url: body.git_url || '',
        base_path: null,
        config_template_id: body.config_template_id || null,
        config_template: "server {\n    listen 80;\n    server_name example.com;\n}",
        startup_script: body.startup_script || '',
        created_at: new Date().toISOString(),
        updated_at: new Date().toISOString(),
        versions: []
      }
      
      projects.push(newProject as any)
      
      return {
        code: 0,
        errorMsg: '',
        data: newProject
      }
    }
  },
  
  // 更新项目
  {
    url: '/api/v1/projects/:id',
    method: 'put',
    response: (req: any) => {
      const { id } = req.params
      const { body } = req
      
      const projectIndex = projects.findIndex(p => p.id === Number(id))
      
      if (projectIndex >= 0) {
        const updatedProject = {
          ...projects[projectIndex],
          ...body,
          updated_at: new Date().toISOString()
        }
        
        projects[projectIndex] = updatedProject as any
        
        return {
          code: 0,
          errorMsg: '',
          data: updatedProject
        }
      } else {
        return {
          code: 404,
          errorMsg: `项目ID ${id} 不存在`,
          data: null
        }
      }
    }
  },
  
  // 删除项目
  {
    url: '/api/v1/projects/:id',
    method: 'delete',
    response: (req: any) => {
      const { id } = req.params
      const projectIndex = projects.findIndex(p => p.id === Number(id))
      
      if (projectIndex >= 0) {
        projects.splice(projectIndex, 1)
        
        return {
          code: 0,
          errorMsg: '',
          data: {
            success: true
          }
        }
      } else {
        return {
          code: 404,
          errorMsg: `项目ID ${id} 不存在`,
          data: null
        }
      }
    }
  },
  
  // 上传项目版本
  {
    url: '/api/v1/projects/:id/versions',
    method: 'post',
    response: (req: any) => {
      const { id } = req.params
      const { body } = req
      
      const projectIndex = projects.findIndex(p => p.id === Number(id))
      
      if (projectIndex >= 0) {
        const project = projects[projectIndex]
        const versionId = (project.versions?.length || 0) + 1
        
        const newVersion = {
          id: versionId,
          version: body.version,
          md5_hash: faker.string.alphanumeric(32),
          notes: body.notes || '',
          is_current: false,
          created_at: new Date().toISOString()
        }
        
        if (project.versions) {
          project.versions.push(newVersion as any)
        } else {
          project.versions = [newVersion as any]
        }
        
        return {
          code: 0,
          errorMsg: '',
          data: newVersion
        }
      } else {
        return {
          code: 404,
          errorMsg: `项目ID ${id} 不存在`,
          data: null
        }
      }
    }
  },
  
  // 获取项目脚本
  {
    url: '/api/v1/projects/:id/script',
    method: 'get',
    response: (req: any) => {
      const { id } = req.params
      const project = projects.find(p => p.id === Number(id))
      
      if (project) {
        return {
          code: 0,
          errorMsg: '',
          data: {
            id: Number(id),
            project_id: Number(id),
            content: project.startup_script || '#!/bin/bash\necho "No script content"',
            created_at: project.created_at,
            updated_at: project.updated_at
          }
        }
      } else {
        return {
          code: 404,
          errorMsg: `项目ID ${id} 不存在`,
          data: null
        }
      }
    }
  },
  
  // 更新项目脚本
  {
    url: '/api/v1/projects/:id/script',
    method: 'put',
    response: (req: any) => {
      const { id } = req.params
      const { body } = req
      const projectIndex = projects.findIndex(p => p.id === Number(id))
      
      if (projectIndex >= 0) {
        // 更新项目的脚本内容
        projects[projectIndex].startup_script = body.content || ''
        projects[projectIndex].updated_at = new Date().toISOString()
        
        return {
          code: 0,
          errorMsg: '',
          data: {
            id: Number(id),
            project_id: Number(id),
            content: body.content || '',
            created_at: projects[projectIndex].created_at,
            updated_at: projects[projectIndex].updated_at
          }
        }
      } else {
        return {
          code: 404,
          errorMsg: `项目ID ${id} 不存在`,
          data: null
        }
      }
    }
  }
]
